package com.zjsru.binaryeasy;

/**
 * @Author: likew
 * @Date: 2022/8/6
 * 统计有序矩阵中的负数
 *
 * 输入：grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
 * 输出：8
 * 解释：矩阵中共有 8 个负数。
 *
 */
public class CountNegatives {
    public int countNegatives(int[][] grid) {
        int cout = 0;
        for (int i = 0; i < grid.length; i++) {
            int length = grid[i].length, left = 0, right = length;
            while (left < length && left <= right) {
                int mid = (left + right) >>> 1, tmp = grid[i][mid];
                if (tmp >= 0) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
            cout+=(length-left);
        }
        return cout;
    }
}
